package cn.net.mugui.net.conf;

import cn.net.mugui.net.dao.Dao;
import com.mugui.base.base.Autowired;
import com.mugui.base.base.Component;

import java.util.List;

@Component
public class SysConf {

    @Autowired
    private Dao dao;

    public SysConfBean save(String key, String value) {
        return save(key, value, null);
    }

    public SysConfBean save(String key, String value, String detail) {
        SysConfBean select = dao.select(new SysConfBean().setKey(key));
        if (select == null) {
            select = new SysConfBean().setKey(key).setValue(value).setDetail(detail);
            select = dao.save(select);
            return select;
        }
        select.setValue(value).setDetail(detail);
        dao.updata(select);
        return select;
    }

    public SysConfBean get(String key, String defaultValue) {
        SysConfBean sysConfBean = get(key);
        if (sysConfBean == null) {
            sysConfBean = save(key, defaultValue);
        }
        return sysConfBean;
    }

    public SysConfBean get(String key) {
        return dao.select(new SysConfBean().setKey(key));
    }

    public List<SysConfBean> list() {
        return dao.selectList(new SysConfBean());
    }

    public String value(String key) {
        SysConfBean sysConfBean = get(key);
        if (sysConfBean != null) {
            return sysConfBean.getValue();
        }
        return null;
    }

    public String value(String key, String defualtValue) {
        return get(key, defualtValue).getValue();
    }
}
